<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
            <el-form-item label="上报人" prop="reportedBy">
                <el-input
                        v-model="queryParams.reportedBy"
                        placeholder="请输入上报人"
                        clearable
                        @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="缺陷上报时间" prop="reportedAt">
                <el-date-picker clearable
                                v-model="queryParams.reportedAt"
                                type="datetime"
                                value-format="YYYY-MM-DD HH:mm:ss"
                                placeholder="请选择缺陷上报时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        plain
                        icon="Plus"
                        @click="handleAdd"
                        v-hasPermi="['ar_check:defects:add']"
                >新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="warning"
                        plain
                        icon="Download"
                        @click="handleExport"
                        v-hasPermi="['ar_check:defects:export']"
                >导出
                </el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="defectsList">
            <el-table-column label="设备编号" align="center" prop="deviceId"/>
            <el-table-column label="问题描述" align="center" prop="description"/>
            <el-table-column label="上报人" align="center" prop="reportedBy"/>
            <el-table-column label="缺陷上报时间" align="center" prop="reportedAt" width="180">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.reportedAt, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="处理班组" align="center" prop="assignedTeam"/>
            <el-table-column label="缺陷状态" align="center" prop="status">
                <template #default="{row}">
                    <dict-tag :options="repair_status" :value="row.status"/>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                               v-hasPermi="['ar_check:defects:edit']">派遣
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改缺陷设备对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="defectsRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="设备ID" prop="deviceId">
                    <el-input v-model="form.deviceId" placeholder="请输入设备ID"/>
                </el-form-item>
                <el-form-item label="问题描述" prop="description">
                    <el-input v-model="form.description" placeholder="请输入问题描述"/>
                </el-form-item>
                <el-form-item label="上报人" prop="reportedBy">
                    <el-input v-model="form.reportedBy" placeholder="请输入上报人"/>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>

        <!--派遣-->
        <el-dialog :title="title" v-model="visit" width="500px" append-to-body>
            <el-form ref="defectsRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="选择班组" prop="group">
                    <el-select v-model="form.assignedTeam" clearable placeholder="请选择班组"
                               @change="checkClick(form)">
                        <el-option v-for="item in group" :key="item.teamId" :label="item.teamName"
                                   :value="item.teamId" :disabled="item.disable"/>
                    </el-select>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import { listDefects, getDefects, delDefects, addDefects, updateDefects, dispatchRepair } from '@/api/ar_check/defects'
import { getTeamListAll } from '@/api/ar_check/team.js'
import DictTag from '@/components/DictTag/index.vue'

defineOptions({
    name: 'Defects'
})

const { proxy } = getCurrentInstance()
const { repair_status } = proxy.useDict('repair_status')
const defectsList = ref([])
const open = ref(false)
const visit = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')
const group = ref([])

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        reportedBy: null,
        reportedAt: null,
        doTime: null,
        status: null,
    },
    rules: {
        deviceId: [
            { required: true, message: '设备ID不能为空', trigger: 'blur' }
        ],
        description: [
            { required: true, message: '问题描述不能为空', trigger: 'blur' }
        ],
        reportedBy: [
            { required: true, message: '上报人不能为空', trigger: 'blur' }
        ],
        reportedAt: [
            { required: true, message: '缺陷上报时间不能为空', trigger: 'blur' }
        ],
        status: [
            { required: true, message: '缺陷状态不能为空', trigger: 'change' }
        ],
        doTime: [
            { required: true, message: '派遣时间不能为空', trigger: 'blur' }
        ],
    }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询缺陷设备列表 */
function getList() {
    loading.value = true
    listDefects(queryParams.value).then(response => {
        defectsList.value = response.rows
        total.value = response.total
        loading.value = false
    })
}

// 取消按钮
function cancel() {
    open.value = false
    visit.value = false
    reset()
}

// 表单重置
function reset() {
    form.value = {
        defectId: null,
        deviceId: null,
        description: null,
        reportedBy: null,
        reportedAt: null,
        doTime: null,
        assignedTeam: null,
        status: null,
        createBy: null,
        createTime: null
    }
    proxy.resetForm('defectsRef')
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1
    getList()
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm('queryRef')
    handleQuery()
}


/** 新增按钮操作 */
function handleAdd() {
    reset()
    open.value = true
    title.value = '添加缺陷设备'
}

function checkClick(row) {
    console.log(row)
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset()
    const _defectId = row.defectId || ids.value
    getDefects(_defectId).then(response => {
        form.value = response.data
        visit.value = true
        title.value = '派遣班组'
        getTeam()
    })
}

async function getTeam() {
    const { data } = await getTeamListAll()
    console.log(data)
    group.value = data
}


/** 提交按钮 */
async function submitForm() {
    const valid = await proxy.$refs['defectsRef'].validate()
    if (valid) {
        if (form.value.defectId != null) {
            await dispatchRepair(form.value)
            proxy.$modal.msgSuccess('派遣成功')
            visit.value = false
            open.value = false
            getList()
        } else {
            addDefects(form.value).then(response => {
                proxy.$modal.msgSuccess('新增成功')
                open.value = false
                getList()
            })
        }
    }
}

/** 删除按钮操作 */
function handleDelete(row) {
    const _defectIds = row.defectId || ids.value
    proxy.$modal.confirm('是否确认删除缺陷设备编号为"' + _defectIds + '"的数据项？').then(function () {
        return delDefects(_defectIds)
    }).then(() => {
        getList()
        proxy.$modal.msgSuccess('删除成功')
    }).catch(() => {
    })
}


/** 导出按钮操作 */
function handleExport() {
    proxy.download('ar_check/defects/export', {
        ...queryParams.value
    }, `defects_${new Date().getTime()}.xlsx`)
}

getList()
</script>
